﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Credit_Teacher_Reprots_GoodTeacherCause : ContainerBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnFlash_Click(object sender, EventArgs e)
    {
        int schoolYear = 0;
        int term = 0;
        if (!Int32.TryParse(txtSchoolYear.Text, out schoolYear))
            return;
        if (!Int32.TryParse(txtTerm.Text, out term))
            return;
        IList<GoodGradeCauseView> resultList = Container<ITeacherEvalTypeCatchLogic>.Instance.FindGoodGradeCause(SchoolMark, schoolYear, term);

        CreateTable(resultList);
    }


    private void CreateTable(IList<GoodGradeCauseView> resultList)
    {
        Dictionary<int, String> typePair = new Dictionary<int, string>();
        foreach (GoodGradeCauseView view in resultList)
        {
            if (!typePair.ContainsKey(view.TypeID))
                typePair.Add(view.TypeID, view.TypeName);
        }

        int width = 0;
        #region 构造表头
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "年级";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "科目";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hrow.Cells.Add(hcell);

        foreach (KeyValuePair<int, string> pair in typePair)
        {
            hcell = new TableHeaderCell();
            hcell.Text = pair.Value;
            hcell.Style[HtmlTextWriterStyle.Width] = "120px";
            width += 120;
            hrow.Cells.Add(hcell);
        }

        hcell = new TableHeaderCell();
        hcell.Text = "总分";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hrow.Cells.Add(hcell);

        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
        #endregion

        if (resultList.Count < 1)
            return;

        #region 构造内容
        TableRow row = new TableRow();
        TableCell cell;
        int oldGrade = -1;
        int oldCauseID = -1;
        double sumScore = 0;
        Dictionary<int, double> scoreList = new Dictionary<int, double>();
        foreach (GoodGradeCauseView view in resultList)
        {
            if (view.Grade != oldGrade || view.CauseID != oldCauseID)
            { // 新行
                if (oldGrade != -1)
                { // 非第一行
                    sumScore = 0;
                    foreach (KeyValuePair<int, string> pair in typePair)
                    {
                        cell = new TableCell();
                        if (scoreList.ContainsKey(pair.Key))
                        {
                            cell.Text = scoreList[pair.Key].ToString();
                            sumScore += scoreList[pair.Key];
                        }
                        row.Cells.Add(cell);
                    }
                    cell = new TableCell();
                    cell.Text = sumScore.ToString();
                    row.Cells.Add(cell);
                    tbResult.Rows.Add(row);
                }
                row = new TableRow();
                cell = new TableCell();
                cell.Text = ConvertGrade(view.Grade);
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = String.Format("{0}组", view.CauseName);
                row.Cells.Add(cell);
                scoreList = new Dictionary<int, double>();
            }
            if (!scoreList.ContainsKey(view.TypeID))
                scoreList.Add(view.TypeID, view.Score);
            else
                scoreList[view.TypeID] += view.Score;
            oldGrade = view.Grade;
            oldCauseID = view.CauseID;
        }
        // 计算最后一行
        sumScore = 0;
        foreach (KeyValuePair<int, string> pair in typePair)
        {
            cell = new TableCell();
            if (scoreList.ContainsKey(pair.Key))
            {
                cell.Text = scoreList[pair.Key].ToString();
                sumScore += scoreList[pair.Key];
            }
            row.Cells.Add(cell);
        }
        cell = new TableCell();
        cell.Text = sumScore.ToString();
        row.Cells.Add(cell);
        tbResult.Rows.Add(row);
        #endregion
    }

    private String ConvertGrade(int grade)
    {
        switch (grade)
        { 
            case 1:
                return "一年级";
            case 2:
                return "二年级";
            case 3:
                return "三年级";
            case 4:
                return "四年级";
            case 5:
                return "五年级";
            case 6:
                return "六年级";
            case 7:
                return "初一";
            case 8:
                return "初二";
            case 9:
                return "初三";
            case 10:
                return "高一";
            case 11:
                return "高二";
            case 12:
                return "高三";
        }
        return "";
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "download\\" + DateTime.Now.Ticks.ToString() + "_profile.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        ExcelUtil.ExcelExport(save_path, DateTime.Now.Ticks.ToString() + "优秀备课组档案", 1, 0, cells);
        return "";
    }
}
